/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package adapdateurs;

import HotelsData.Hotel;
import HotelsData.Hotels;
import HotelsData.Pays;
import HotelsData.Ville;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author BENAMMAR Riyadh
 */
public class HotelDeXml implements Adapdateur{
    
    Hotels hotels=new Hotels();
    @Override
    public String traduire(String requete) {
        /*switch(requete){
            case "liste de tous les hotels":return "getHotelsList";
            case "liste de toutes les villes contenant des hotels": return "getAllVillesHotels";
        }*/
        return requete;
    }

    @Override
    public List<Hotel> executerRequete(String requete) {
        /*switch(requete){
            case "getHotelsList": return getHotelsList();
            case "getAllVillesHotels": return getAllVillesHotels();
        }*/
        String [] champs=requete.split(":");
        return getTousHotelsParVille(champs[1]);
    }

    private List<String> getHotelsList() {
        try {
            List<String> aRetourner=new ArrayList<>();
            JAXBContext jc = JAXBContext.newInstance("HotelsData");
           Unmarshaller unmarshaller = jc.createUnmarshaller();
            Hotels hotels = (Hotels) unmarshaller.unmarshal(new File("ressources/Hotel.xml"));
            //Hotels hotels=new Hotels();
            List<Pays> pays=hotels.getPays();
            for(Pays p:pays){
                List<Ville> villes = p.getVille();
                for(Ville v:villes){
                    List<Hotel> hotel=v.getHotel();
                    for (Hotel h:hotel)
                    aRetourner.add(h.getNomHotel());
                }
            }
            return aRetourner;
        } catch (JAXBException ex) {
            Logger.getLogger(HotelDeXml.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    private List getAllVillesHotels() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    public List<Hotel> getTousHotelsParVille(String ville){
        List<Hotel> resultat=new ArrayList<>();
        try {
            JAXBContext jc = JAXBContext.newInstance("HotelsData");
           Unmarshaller unmarshaller = jc.createUnmarshaller();
            Hotels hotels = (Hotels) unmarshaller.unmarshal(new File("ressources/Hotel.xml"));
            //Hotels hotels=new Hotels();
            List<Pays> pays=hotels.getPays();
            for(Pays p:pays){
                List<Ville> villes = p.getVille();
                for(Ville v:villes){
                    if(!v.getNom().equalsIgnoreCase(ville))continue;
                    List<Hotel> hotel=v.getHotel();
                    for (Hotel h:hotel)
                    resultat.add(h);
                }
            }
        } catch (JAXBException ex) {
            Logger.getLogger(HotelDeXml.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resultat;
    }
    
    public static void main(String[] args) {
        HotelDeXml h=new HotelDeXml();
        List<Hotel> listeH=h.executerRequete("getHotelsList");
        System.out.println("liste de tous les hotels: \n"+listeH.toString());
    }
    
}
